﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using facebook.Schema;
using facebook.web;
using facebook;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Xml;

namespace FacebookApp
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
                //FriendList1.Friends = Master.API.friends.getUserObjects();
                //UserProfile1.User = Master.API.users.getInfo(Master.API.uid);
                //IList<album> albums = Master.API.photos.getAlbums();
                //facebook.Schema.user userinfo = Master.API.users.getInfo(Master.API.uid);
                //Label1.Text = "Hi, " + userinfo.first_name + userinfo.uid + "/" + Master.API.uid;
                //Image1.ImageUrl = userinfo.pic;
                //foreach (user u in FriendList1.Friends)
                //{
                //    DateTime bdate = u.birthday_date;
                //    DropDownList1.Items.Add(u.first_name);
                   
                //}
        //        Literal1.Text = "<div id=\"fb-root\"><script src=\"" + "http://connect.facebook.net/en_US/all.js#appId=290146691073914&amp;xfbml=1\">" +
        //"</script></div><div class=\"fb-comments\" data-href=\"http://www.fbapps.somee.com\" " + "data-num-posts=\"10\" data-width=\"490\" data-height=\"50\"></div>";
                

                //WebMsgBox.Show(ConfigurationSettings.AppSettings["fb_domain"].ToString());


                //js để lấy thông tin user lưu xuống file
                //StringBuilder sb = new StringBuilder();
               // ConfigurationSettings.AppSettings["fb_domain"] = Request.PhysicalApplicationPath.ToString();                                               
                //sb.Append("<script type='text/javascript' src='/Scripts/UserInfo.js'></script>");

                //if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "loaddata"))
                //{
                //    Page.ClientScript.RegisterStartupScript(typeof(Page), "loaddata", sb.ToString());
                //}
			}

		}

		protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
		{
            //IList<user> Friends = Master.API.friends.getUserObjects();
            //// Use the FacebookService Component to populate Friends
            //Image2.ImageUrl = Friends[DropDownList1.SelectedIndex].pic;
		}

        protected void sd_Click(object sender, EventArgs e)
        {
            //string header = "Sản phẩm mới";

            //attachment attachment = new attachment();

            //attachment.caption = "http://apps.facebook.com/e_store/";
            //attachment.name = "Tên sản phẩm";
            //attachment.href = "http://apps.facebook.com/e_store/Pages/ProductDetail.aspx?Category_Id=1&Id=5";
            //attachment.description = "Mô tả sản phẩm";
            //attachment.properties = new attachment_property()
            //{
            //    category = new attachment_category()
            //    {
            //        href = "http://apps.facebook.com/e_store/?auth_token=b12ac0a2603c7ab15d0e73338a21e01f",
            //        text = "humor"
            //    },
            //    ratings = "5 stars"
            //};
            //attachment.media = new List<attachment_media>(){new attachment_media_image()
            //                        {
            //                            src = "http://fbapps.somee.com/Styles/Images/Koala.jpg",
            //                            href = "http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/"
            //                        }};
            //List<action_link> action_link = new List<action_link>() { new action_link() { 
            //    text = "Become a good guy",
            //    href = "http://apps.facebook.com/e_store/" } };

            //Master.API.stream.publish(header, attachment, action_link, Master.API.uid.ToString(), 1814236399);

           //WebMsgBox.Show( Master.API.fql.query(
            //fql f = new fql(Master.API);
            //lbt.Text = Master.API.fql.query("SELECT name,email, current_location FROM user WHERE uid = " + Master.API.uid.ToString());
            ////lbt.Text = s;

            //StringReader data = new StringReader(Master.API.fql.query("SELECT name,email, current_location FROM user WHERE uid = " + Master.API.uid.ToString()));

            //XmlTextReader reader = new XmlTextReader(data);
            //XmlDocument doc = new XmlDocument();
            //doc.Load(reader);
            //XmlNode node = doc.GetElementsByTagName("email").Item(0);            
            //if(node.HasChildNodes)
            //    WebMsgBox.Show(node.FirstChild.Value);

           // if (doc.GetElementsByTagName("current_location").Count > 0)
                //WebMsgBox.Show(doc.GetElementsByTagName("current_location").Item(0).FirstChild.Value);
            //while (reader.Read())
            //{
            //    switch (reader.NodeType)
            //    {
            //        case XmlNodeType.Element: // The node is an Element.//kiem tra fiel tra ve là name, email,...
            //            WebMsgBox.Show(reader.Name +"1");
            //            //Console.Write("<" + reader.Name);

            //            while (reader.MoveToNextAttribute()) // Read attributes.
            //                //Console.Write(" " + reader.Name + "='" + reader.Value + "'");
            //                WebMsgBox.Show(reader.Name + "='" + reader.Value + "'2");
            //            //Console.Write(">");
            //            //Console.WriteLine(">");
            //            break;
            //        case XmlNodeType.Text: //Display the text in each element.
            //            WebMsgBox.Show(reader.Value+"3");
            //           // Console.WriteLine(reader.Value);
            //            break;
            //        case XmlNodeType.EndElement: //Display end of element.
            //            WebMsgBox.Show(reader.Value+"4");
            //            //Console.Write("</" + reader.Name);
            //            //Console.WriteLine(">");
            //            break;
            //    }
            //}

            //API api = new API();
            //api.ApplicationKey = Master.API.ApplicationKey;
            //api.uid = Master.API.uid;
            //api.AuthToken = Master.API.AuthToken;
            //api.Secret = Master.API.Secret;
            //api.SessionKey = Master.API.SessionKey;
            ////api.profile.getInfoOptions("");

            //IList<user> appuser = api.friends.getAppUsersObjects();
            //foreach (user u in appuser)
            //{
            //    WebMsgBox.Show(u.name);
            //    break;
            //}

            //String fbUserId = Master.API.users.getLoggedInUser().ToString();//.Users.GetLoggedInUser().ToString();
            //string fbSigSessionKey = Master.API.SessionKey;
            //Dictionary<string, string> parameterList = new Dictionary<string, string>();
            //parameterList.Add("method", "Users.getInfo");
            //parameterList.Add("uids", fbUserId);
            //parameterList.Add("fields", "email,first_name,last_name,name,pic,sex,current_location," +
            //"hometown_location,affiliations,has_added_app,locale,birthday_date");
            //parameterList.Add("session_key", fbSigSessionKey);

            //string result = Master.API.users.getInfo(Master.API.uid).email_hashes.email_hashes_elt[0];
            //WebMsgBox.Show(result);

            //string s = Master.API.fql.query("SELECT uid, name, pic_square FROM user WHERE uid = 1814236399 ");
            //s = "123";
            //WebMsgBox.Show(Master.API.uid.ToString());
            //List<string> data = GetUserInfo.GetAll(Master.API.uid.ToString());
            ////WebMsgBox.Show(data.Count.ToString());
            //if (data.Count > 0)
            //    WebMsgBox.Show(data[0].ToString());
        }

        
}
}

//Facebook.FacebookApp facebookApp = new Facebook.FacebookApp();
//               if (facebookApp.Session != null)
//               {
//                   var facebookUser = facebookApp.Api("me") as JsonObject;
 
//                   if (facebookUser.ContainsKey("name") && facebookUser["name"] != null)
//                       sb.AppendFormat("<strong>You are logged in as:</strong> {0}</br>", facebookUser["name"].ToString());
 
//                   if (facebookUser.ContainsKey("gender") && facebookUser["gender"] != null)
//                       sb.AppendFormat("Gender: {0}</br>", facebookUser["gender"].ToString());
 
//                   if (facebookUser.ContainsKey("username") && facebookUser["username"] != null)
//                       sb.AppendFormat("Username: {0}</br>", facebookUser["username"].ToString());
 
//                   if (facebookUser.ContainsKey("email") && facebookUser["email"] != null)
//                       sb.AppendFormat("E-mail: {0}</br>", facebookUser["email"].ToString());
//               }
//               ltlFacebookUserInfo.Text = sb.ToString();